﻿using SmallTool.Entity;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;

namespace SmallTool.frm
{
    public partial class frm_Calc : Form
    {
        public frm_Calc()
        {
            InitializeComponent();
        }

        private void btn_calc_Click(object sender, EventArgs e)
        {
            lv_show.Items.Clear();

            DateTime cfgStartTime = dtp_startTime.Value;
            DateTime cfgEndTiime = dtp_endTime.Value;
            DateTime serverOpenTime = dtp_startOpenServer.Value;
            int openInterval = Convert.ToInt32(txt_openInterval.Text);
            int openRounds = Convert.ToInt32(txt_openRounds.Text);
            int startOpenServerPar = Convert.ToInt32(txt_startOpenServerPar.Text);
            double totalSecond = (cfgEndTiime - cfgStartTime).TotalSeconds;

            DateTime dtS = new DateTime();
            DateTime dtE = new DateTime();

            List<TempMsg> list = new List<TempMsg>();
            int actvitityType = rdo_everyWeek.Checked ? 1 : 2;
            if (openRounds > 0)
            {
                for (int i = 1; i <= openRounds; i++)
                {
                    if (actvitityType == 1)
                    {
                        dtS = cfgStartTime.AddHours(openInterval * (i - 1));
                        dtE = dtS.AddSeconds(totalSecond);
                        list.Add(new TempMsg(i, dtS, dtE));
                    }
                    else
                    {
                        dtS = serverOpenTime.Date.AddHours(startOpenServerPar).AddHours(openInterval * (i - 1)).AddHours(cfgStartTime.Hour).AddMinutes(cfgStartTime.Minute).AddSeconds(cfgStartTime.Second);
                        dtE = dtS.AddSeconds(totalSecond);
                        list.Add(new TempMsg(i, dtS, dtE));
                    }
                }
            }

            if (list.Count > 0)
            {
                foreach (var item in list)
                {

                    ListViewItem lvi = new ListViewItem(new string[] { string.Format("第{0}轮", item.Round), item.StartTime.ToString(), item.EndTime.ToString() });
                    if (item.StartTime <= DateTime.Now && item.EndTime >= DateTime.Now)
                    {
                        lvi.BackColor = Color.Red;
                    }
                    lv_show.Items.Add(lvi);
                }
            }
        }

        private void frm_Calc_Load(object sender, EventArgs e)
        {
            dtp_startTime.Value = DateTime.Now.Date;
            dtp_endTime.Value = DateTime.Now.Date.AddDays(1).AddSeconds(-1);
            dtp_startOpenServer.Value = DateTime.Now.Date.AddHours(10);
        }
    }
}
